package com.cloud.pole.shop.service.goods.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cloud.pole.api.user.UserCollectionApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.ListUtils;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.feign.IUserCollectionService;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.vo.goods.GoodsCollectionVo;
import com.cloud.pole.shop.service.goods.GoodsCollectionService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;


/*
*2019年3月26日
*cloud-shop
*author:frank-chen
*/
@Slf4j
@Service
public class GoodsCollectionServiceImpl implements GoodsCollectionService {
	
	@Autowired
	private IUserCollectionService userCollectionService;
	@Autowired 
	private ShopGoodsBiz shopGoodsBiz;
	@Override
	public PageInfo<GoodsCollectionVo> getPageGoodsCollectionList(String token, int pageNum, int pageSize)
			throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		Long userId = Long.parseLong(jwtInfo.getId());
		ApiRestResponse<PageInfo<UserCollectionApi>> apiRestResponse = userCollectionService.getPageGoodsCollectionList(userId, UserCollectionApi.OBJECT_TYPE_ID_GOODS, pageNum, pageSize);
		if(apiRestResponse.getStatus() != CommonConstants.STATUS_OK_CODE) {
			throw new BaseException(apiRestResponse.getStatus(), apiRestResponse.getMessage());
		}
		PageInfo pageInfo = apiRestResponse.getData();
		List<UserCollectionApi> srcList = pageInfo.getList();
		log.info(String.valueOf(srcList));
		List<GoodsCollectionVo> destList = ListUtils.copyListProperties(srcList, GoodsCollectionVo.class);
		if(destList != null) {
			for(GoodsCollectionVo destMsg : destList) {
				ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(Integer.parseInt(destMsg.getObjectId()));			
				destMsg.setGoodsName(shopGoods.getName());
				destMsg.setGoodsImage(shopGoods.getTitleImage());
				destMsg.setGoodsCurrentPrice(shopGoods.getCurrentPrice());
			}
		}
		pageInfo.setList(destList);
		return pageInfo;
	}
}
